package pl.bristleback.scrumtable.actions;

import org.apache.log4j.Logger;
import org.jwebsocket.api.WebSocketConnector;
import org.jwebsocket.token.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.bristleback.scrumtable.services.WidgetService;
import pl.bristleback.scrumtable.utils.SendMessageSupport;
import pl.bristleback.scrumtable.vo.Widget;
import pl.bristleback.server.bristle.actions.AnnotatedRemoteAction;
import pl.bristleback.server.bristle.actions.RemoteAction;
import pl.bristleback.server.bristle.token.Tokenize;
import pl.bristleback.server.bristle.token.Tokenizer;
import pl.bristleback.server.bristle.utils.BristleTokenFactory;

import java.util.ArrayList;

/**
 * //@todo class description
 * <p/>
 * Created on: 13.04.11 16:40 <br/>
 *
 * @author Paweł Machowski
 */
@Controller
@AnnotatedRemoteAction(actionName = "CONNECTION_STARTED_ACTION_NAME")
public class WelcomeAction implements RemoteAction{
  private static Logger log = Logger.getLogger(WelcomeAction.class.getName());

  @Autowired
  private SendMessageSupport sender;

  @Autowired
  private WidgetService widgetService;


  @Tokenize(parameters = {"widgets"}, deepSearch = true)
  private Tokenizer tokenHelper;

  @Override
  public void performAction(WebSocketConnector webSocketConnector, Token token) {
    log.info("logged");

    Token sendToken = BristleTokenFactory.createJsonToken("widgetsList");
    tokenHelper.tokenize(sendToken, new ArrayList<Widget>(widgetService.getAllWidgets()));

    sender.send(sendToken, webSocketConnector);
  }
}